#include "Image.h"

Image::Image()
{
    set_x(0);
    set_y(0);
    frame = 0;
    surface = NULL;
}

Image::~Image()
{
	if(surface != NULL)
        SDL_FreeSurface(surface);
}

void Image::initialize(Artist* artist, std::string filename, int x, int y)
{
    set_x(x);
    set_y(y);
    surface = NULL;
    setImage(*artist, filename);
}

SDL_Rect Image::get_bounds()
{
    return bounds;
}

void Image::set_x(int new_x)
{
    bounds.x=new_x;
}

void Image::set_y(int new_y)
{
    bounds.y=new_y;
}

void Image::setFrame(int fr)
{
    if ((fr > 10*(walkingFrames.size() - 1) ) && walkingFrames.size() >= 1)
        fr = 0;

    frame = fr;
}

void Image::incFrame()
{
    if(++frame > 10*(walkingFrames.size() - 1))
        frame = 0;
}

void Image::rotateSprite(Artist& artist, Direction dir, bool rotate_flipped)
{
    SDL_Rect box;
    box.x = 0; box.y = 0;
    box.w = surface->w;
    box.h = surface->h;

    if(walkingFrames.size()>0)
    {
        box.w = walkingFrames[0].w;
        box.h = walkingFrames[0].h;
    }


    surface = artist.rotate_surface(surface, dir, box);


    if(rotate_flipped)
        horiz_flipped_surface = artist.rotate_surface(horiz_flipped_surface, dir, box);

}

void Image::display(SDL_Surface* screen, bool flipped, int x_offset, int y_offset)
{
    SDL_Rect box = bounds;
    box.x += x_offset;
    box.y += y_offset;
    SDL_Surface* sprite = surface;

    if(flipped)
        sprite = horiz_flipped_surface;

    if(walkingFrames.size() == 0)
        SDL_BlitSurface( sprite, NULL, screen, &box);
    else{
        SDL_BlitSurface( sprite, &walkingFrames[frame/10], screen, &box);
    }
}

void Image::setSurface(SDL_Surface* newSurf)
{
    if(surface != NULL)
        SDL_FreeSurface(surface);

    surface = newSurf;
}

void Image::setImage(Artist& artist, std::string filename, bool flipme, bool magentaKey, bool semitransp)
{

    std::cout << "loading image from " << filename << "\n";

    filename = "sprites/" + filename;

    if(semitransp)
        surface = artist.load_semitransp(filename);
    else if(!magentaKey)
        surface = artist.load_image(filename, 0, 0, 0); //color key black
    else
        surface = artist.load_image(filename);

	if(surface)
	{
		bounds.w = surface->w;
		bounds.h = surface->h;
		std::cout << "image loaded successfully from " << filename << " with dimensions:" << bounds.w << "," << bounds.h << "\n\n";

        if(flipme)
        {
            std::cout << "generating flipped version.\n\n";
            horiz_flipped_surface = artist.flip_surface( surface, FLIP_HORIZONTAL );
        }

	}
	else
	{
		bounds.w = 0;
		bounds.h = 0;
		std::cout << "surface loading failed\n";
	}


}

void Image::setStanding(bool flip)
{
    if(walkingFrames.size() == 4)
    {
        if(!flip)
            frame = 2*10;
        else
            frame = 1*10;
    }
}

void Image::frameSplice(int width, int height)
{
    if(walkingFrames.size() > 0)
        walkingFrames.clear();

    if(!width)
        width = 32;

    for (int i = 0; i < bounds.w / width ; i++)
    {

        SDL_Rect framebox;
        framebox.x = i*width;
        framebox.y = 0;
        framebox.w = width;
        framebox.h = height;
        walkingFrames.push_back(framebox);

    }
    //for 3 frame walking animations, assuming middle should be repeated
    if (walkingFrames.size() == 3)
    {
        SDL_Rect copyFrame = walkingFrames[1];
        walkingFrames.push_back(copyFrame);
    }

}
